define(function(require, exports){
	var $ = require('jquery');
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');

	// 单条频次控制
	var Base = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'class': 'M-PageFrequency',
				'opts': null
			});

			this.Super('init', arguments);
		}
		,afterBuild: function(){
			var tpl = [
				'<div class="mb5">',
					'<span class="M-PageFrequencyType mr10" pub-name="type" pub-mod="@base/common/drop.drop"/>',
					'<span class="M-PageFrequencyInterval mr10" pub-name="interval" pub-mod="@base/common/drop.drop"/>',
					'<span class="M-PageFrequencyInput mr5" pub-name="input" pub-mod="@base/common/input.text"/>',
					'<span class="M-PageFrequencyAfterText">' + LANG('次') + '</span>',
				'</div>',
			].join('');
			this.append(tpl);

			var items = [
				{name: LANG('推广组'), id: 1},
				{name: LANG('创意包'), id: 3},
			];

			var period_items = [
				{name: LANG('每小时'), id: 1},
				{name: LANG('每天'), id: 2},
				{name: LANG('每周'), id: 0},
				{name: LANG('每月'), id: 3},
				{name: LANG('每半年'), id: 4}
			];

			var cfg = this.getConfig();
			if (!cfg.opts) {
				cfg.opts = {
					type: {
						'options': items,
						'width': 120,
						'value': 1,
						'hasSearch': false
					},
					interval: {
						'options': period_items,
						'width': 120,
						'value': 1,
						'hasSearch': false
					},
					input: {
						'value': '',
						'width': 100
					}
				};
			}

			this.buildFromTemplate(cfg.opts);
		}
		,getData: function(){
			var row = {};
			var self = this;
			// 类型选择
			row.Type = self.get('type').getValue('id');
			// 频率选择
			row.Period = self.get('interval').getValue('id');
			// 频率
			row.Frequency = self.get('input').getValue();

			return row;
		}
		,setData: function(data){
			var self = this;
			self.get('type').setValue(data["Type"]);
			self.get('interval').setValue(data["Period"]);
			self.get('input').setValue(data["Frequency"]);

			return self;
		}
		,reset: function(){
			var self = this;
			// 设置默认值
			self.get('type').setValue(1);
			self.get('interval').setValue(1);
			self.get('input').setValue(6);

			return self;
		}
	});

	exports.base = Base;

	// 多条频次控制
	var PlanFrequency = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'class': 'P-setPromoteGroup',
				'view_model': {
					'clickAdd': this.eventAdd
				}
			});

			this.$items = [];
			this.$modPrefix = 'frequency';
			this.$repeatId = 0;

			this.Super('init', arguments);
		}
		,afterBuild: function(){
			var tpl = [
				'<div class="P-frequency">',
				'</div>',
				'<div class="mt10">',
				 	'<span ms-click="clickAdd" class="font-primary cur_p">' + LANG('添加频次控制') + '</span>',
				'</div>'
			].join('');
			this.append(tpl);

			this.buildFromTemplate();
		}
		,afterBuildTemplate: function(){
			this.addItem();
		}
		,eventAdd: function(ev){
			var items = this.$items;
			if (items.length >= 6) {
				pubjs.alert(LANG('已达到条件的最大数量！'));
				return false;
			}
			else {
				this.addItem();
				this.judgeDelete();
			}
			return false;
		}
		,judgeDelete: function(){
			var items = this.$items;
			// 超过一条时，全部都能删除
			if (items.length > 1) {
				items.forEach(function(value){
					value.deleteDom.show();
				});
			}
			else {
				items.forEach(function(value){
					value.deleteDom.hide();
				});
			}

			return this;
		}
		,addItem: function(item){
			var self = this;
			var el = this.getDOM();
			var dom = el.find('.P-frequency');
			var id = ++this.$repeatId;
			var container = $('<div class="P-newFrequency"></div>').appendTo(dom);
			var target = $('<div class="P-newFrequencyFre mr15"/>').appendTo(container);
			var deleteDom = $('<span class="delete font-primary cur_p">' + LANG("删除") + '</span>').appendTo(container);

			self.createAsync(this.$modPrefix + id, 'campaign/modules/newFrequency.base', {
				target: target
			},function(mod){
				if (item) {
					mod.setData(item);
				}
			});

			self.uiBind(deleteDom, 'click', id, this.eventRemoveItem);

			// 初次构建时，只有一条，不能删除
			if (self.$items.length < 1) {
				deleteDom.hide();
			}

			self.$items.push({
				'id': id,
				'container': container,
				'deleteDom': deleteDom,
				'modName': this.$modPrefix + id
			});

			return this;
		}
		,eventRemoveItem: function(ev){
			var items = this.$items;
			var id = ev.data;
			this.removeItem(id);
			// 只剩一条时，不可删除
			if (items.length <= 1) {
				items[0].deleteDom.hide();
			}
			return false;
		}
		,removeItem: function(id){
			var cs = this.$;
			var items = this.$items;
			this.empty(id);

			return this;
		}
		,getData: function(){
			var cs = this.$;
			var data = [];
			this.$items.forEach(function(item){
				data.push(cs[item.modName].getData());
			});

			return data;
		}
		,setData: function(data){
			var self = this;
			// reset时会新建一条数据，清空后再setData
			this.empty();
			data.forEach(function(item){
				self.addItem(item);
			});
			this.judgeDelete();

			return this;
		}
		// 有id，清空单条，否则清空全部
		,empty: function(id){
			var cs = this.$;

			if (id) {
				this.$items.forEach(function(item){
					if (item.id && (id === item.id)) {
						item.container.remove();
						cs[item.modName].destroy();
					}
				});
				util.remove(this.$items, id, 'id');
			}
			else {
				this.$items.forEach(function(value){
					value.container.remove();
					cs[value.modName].destroy();
				});
				this.$items = [];
			}

			return this;
		}
		, reset: function(){
			// 清空后，添加一条默认数据
			this.empty();
			this.addItem();
			this.vm.reset();

			return this;
		}
	});

	exports.planFrequency = PlanFrequency;
});